package com.ml.mall.api.analytics;

import com.ml.mall.annotations.RequiresRoutings;
import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryOneReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryReportPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQuerySpentCategoryReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryPageReq;
import com.ml.mall.bean.req.supplier.SupplierPurchaseQueryOneReportReq;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.service.AnalyticsSupplierService;
import com.ml.mall.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by kyyang on 2021/6/8 21:39
 */
@Api(tags = "【analytics】卖家")
@RestController
@RequestMapping("/supplier")
@Slf4j
public class AnalyticsSupplierController extends BaseController {



    @Autowired
    private AnalyticsSupplierService analyticsSupplierService;


    @RequestMapping(method = RequestMethod.POST, path = "/purchase/category/query_page")
    @ApiOperation("查询某一买家 消费一级目录")
//    @RequiresPermissions(PermissionConst.SUPPLIER_PURCHASE_CATEGORY_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_PURCHASE_CATEGORY_QUERY_PAGE)
    public ApiRestResult supplierPurchaseCategoryQueryPage(@Valid @RequestBody PurchaseQuerySpentCategoryReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierPurchaseCategoryQueryPage(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/category/query_top")
    @ApiOperation("查询卖家目录销售报告top")
//    @RequiresPermissions(PermissionConst.SUPPLIER_CATEGORY_QUERY_TOP)
//  @RequiresRoutings(PermissionConst.SUPPLIER_CATEGORY_QUERY_TOP)
    public ApiRestResult supplierCategoryQueryTop(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierCategoryQueryTop(req));
    }
    @RequestMapping(method = RequestMethod.POST, path = "/country/query_report")
    @ApiOperation("卖家查询洲属销售报告")
//    @RequiresPermissions(PermissionConst.SUPPLIER_COUNTRY_QUERY_REPORT)
//  @RequiresRoutings(PermissionConst.SUPPLIER_COUNTRY_QUERY_REPORT)
    public ApiRestResult supplierCountryQueryReport(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierCountryQueryReport(req));
    }
    @RequestMapping(method = RequestMethod.POST, path = "/category/query_page")
    @ApiOperation("按照一级分组查询销售额")
//    @RequiresPermissions(PermissionConst.SUPPLIER_CATEGORY_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_CATEGORY_QUERY_PAGE)
    public ApiRestResult supplierCategoryQueryPage(@Valid @RequestBody AdminCategoryQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierCategoryQueryPage(req));
    }
    @RequestMapping(method = RequestMethod.POST, path = "/category/query_one")
    @ApiOperation("查询某一目录统计详情")
//    @RequiresPermissions(PermissionConst.SUPPLIER_CATEGORY_QUERY_ONE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_CATEGORY_QUERY_ONE)
    public ApiRestResult supplierCategoryQueryOne(@Valid @RequestBody AdminCategoryQueryOneReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierCategoryQueryOne(req));
    }
    @RequestMapping(method = RequestMethod.POST, path = "/category/query_one_subs_report")
    @ApiOperation("查询某一目录子目录的销售额统计；参照管理员；子目录分页；只不过此处要求是查询的订单是该卖家卖出的")
//    @RequiresPermissions(PermissionConst.SUPPLIER_CATEGORY_QUERY_ONE_SUBS_REPORT)
//  @RequiresRoutings(PermissionConst.SUPPLIER_CATEGORY_QUERY_ONE_SUBS_REPORT)
    public ApiRestResult supplierCategoryQueryOneSubsReport(@Valid @RequestBody AdminCategoryQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierCategoryQueryOneSubsReport(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_top")
    @ApiOperation("查询卖家商品变体销售报告topN")
//    @RequiresPermissions(PermissionConst.SUPPLIER_VARIANT_QUERY_TOP)
//  @RequiresRoutings(PermissionConst.SUPPLIER_VARIANT_QUERY_TOP)
    public ApiRestResult supplierVariantQueryTop(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierVariantQueryTop(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_page")
    @ApiOperation("卖家分页查询变体汇总")
//    @RequiresPermissions(PermissionConst.SUPPLIER_VARIANT_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_VARIANT_QUERY_PAGE)
    public ApiRestResult supplierVariantQueryPage(@Valid @RequestBody VariantQueryPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierVariantQueryPage(req));
    }
    @RequestMapping(method = RequestMethod.POST, path = "/variant/query_one")
    @ApiOperation("卖家查询某一个变体的时间范围内的销售报告")
//    @RequiresPermissions(PermissionConst.SUPPLIER_VARIANT_QUERY_ONE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_VARIANT_QUERY_ONE)
    public ApiRestResult supplierVariantQueryOne(@Valid @RequestBody VariantQueryOneReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierVariantQueryOne(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/order/query_report")
    @ApiOperation("卖家订单报告")
//    @RequiresPermissions(PermissionConst.SUPPLIER_ORDER_REPORT)
//  @RequiresRoutings(PermissionConst.SUPPLIER_ORDER_REPORT)
    public ApiRestResult supplierOrderQueryReport(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierOrderQueryReport(req, null));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/purchase/query_top")
    @ApiOperation("查询卖价查询自己店铺买家报告")
//    @RequiresPermissions(PermissionConst.SUPPLIER_PURCHASE_QUERY_TOP)
//  @RequiresRoutings(PermissionConst.SUPPLIER_PURCHASE_QUERY_TOP)
    public ApiRestResult supplierPurchaseQueryTop(@Valid @RequestBody BaseDateRangeReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierPurchaseQueryTop(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/purchase/query_one_report")
    @ApiOperation("查询卖价查询自己店铺买家报告")
//    @RequiresPermissions(PermissionConst.SUPPLIER_PURCHASE_QUERY_ONE_REPORT)
//  @RequiresRoutings(PermissionConst.SUPPLIER_PURCHASE_QUERY_ONE_REPORT)
    public ApiRestResult supplierPurchaseQueryOneReport(@Valid @RequestBody SupplierPurchaseQueryOneReportReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierPurchaseQueryOneReport(req));
    }

    @RequestMapping(method = RequestMethod.POST, path = "/purchase/query_page")
    @ApiOperation("卖家分页查询自己下属商品辩题销售业绩")
//    @RequiresPermissions(PermissionConst.SUPPLIER_PURCHASE_QUERY_PAGE)
//  @RequiresRoutings(PermissionConst.SUPPLIER_PURCHASE_QUERY_PAGE)
    public ApiRestResult supplierPurchaseQueryPage(@Valid @RequestBody PurchaseQueryReportPageReq req) throws Exception {
        int dateDiff = DateUtils.getDateDiff(req.getTimeBegin(), req.getTimeEnd());
        if(CommonConsts.MAX_REPORT_DIFF_DAYS<dateDiff){
            log.error("当前时间范围天数为：[{}],超过上限 {} 天",dateDiff,CommonConsts.MAX_REPORT_DIFF_DAYS);
            return ApiRestResult.err("Jarak masa yang dipilih：["+dateDiff+"],telah melebihi 30 hari","400");
        }
        return ApiRestResult.ok(analyticsSupplierService.supplierPurchaseQueryPage(req));
    }


}
